package algorithm.leetcode;

/**
 * 两个字符串str1和str2
 * str1="ADGESDFAEG";
 * str2="DERSFG"
 * 判断str2是否是str的子集
 *
 * @author: fengpeng
 * @date: 2019/1/22  下午11:16
 */
public class T1 {

    public void solve1(String str1,String str2){
        boolean flag = true;
        for (char c : str2.toCharArray() ){
            if ( -1 ==str1.indexOf(c) ){
                flag = false;
                break;
            }
        }
        if (flag){
            System.out.println("str2是str1的子集！");
        }else{
            System.out.println("str2不是str1的子集！");
        }
    }

    public void solve2(String str1,String str2){
        boolean flag = true;
        int[]  hashArray= new int[26];
        for (char c : str1.toCharArray()){
            int i = c - 'A';
            hashArray[i] += 1;
        }
        for(char c : str2.toCharArray()){
            int i = c - 'A';
            if (0 == hashArray[i]){
                flag = false;
                break;
            }
        }
        if (flag){
            System.out.println("str2是str1的子集！");
        }else{
            System.out.println("str2不是str1的子集！");
        }
    }

    public static  void main(String[] args){
        T1 t1= new T1();
        String str1 = "ADGESDFAEGR";
        String str2 = "DERSFGX";
        t1.solve2(str1,str2);
    }

}
